package com.niezhipeng.simple;

import com.niezhipeng.utils.ConnectFactoryUtils;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;

/**
 * @author Nie ZhiPeng
 */
public class Send {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] argv) throws Exception {
        // 新建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        ConnectFactoryUtils.setFactoryConfig(factory);

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 声明一个队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "NZP simple Hello World!";
            // RabbitMQ 消息传递模型的核心思想是生产者从不直接向队列发送任何消息
            // 这里看似没有交换机，实际上我们使用的是默认交换机，我们用空字符串 ( "" ) 标识它。
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}
